package com.uchengwang.ui.utils;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.uchengwang.ui.UChengApplication;

/**
 * Created by U城网 on 2017/3/27.
 *
 * 高德地图定位 搜索 mapView
 */

public class MapManager {

    private static MapManager mapManager;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;

    private MapManager(){}

    public static MapManager getMapManager() {
        if (mapManager == null){
            mapManager = new MapManager();
        }

        return mapManager;
    }

    public void startLocation(AMapLocationListener listener){
        mLocationClient = new AMapLocationClient(UChengApplication.getContext());
        mLocationOption = new AMapLocationClientOption();

        //设置定位回调监听
        mLocationClient.setLocationListener(listener);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(false);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }

    public void stopLocation(){
        if (mLocationClient != null){
            mLocationClient.stopLocation();//停止定位后，本地定位服务并不会被销毁
        }

    }

    public void desLocation(){
        if (mLocationClient != null){
            mLocationClient.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
        }
    }
}
